<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Request;
use Myredis\Myredis;

class Member extends Admincontroller
{
    /**
     *显示用户列表
     * @return \think\Response
     */
    public function index()
    {
        $res = Db::table('xo_user')->field(['id', 'tel', 'nickname', 'status','login_time','dw_num'])->select();
        return view('index', [
            'data' => $res,
            'title' => '会员列表'
        ]);
    }

    /**
     * 后台会员激活/禁用操作
     * @return \think\response\Json|void
     */
    public function action()
    {
        if (!Request::instance()->isAjax()) {
            return $this->error('小伙子迷了路', url('admin/index/index'));
        }
        $id = Request::instance()->get('id');
        $res = Db::table('xo_user')->where(['id' => $id])->update(['status' => 1]);
        $tel = Db::table('xo_user')->field('tel')->find($id);
        if ($res) {
            $data = [
                'status' => true,
                'info' => '会员' . $id . '激活成功'
            ];
            $redis = Myredis::hget('userLogin', $tel['tel']);
            if ($redis) {
                $redis['status'] = 1;
                $res = Myredis::hset('userLogin', $tel['tel'], $redis);
            }

        } else {
            $res = Db::table('xo_user')->where(['id' => $id])->update(['status' => 0]);
            if ($res) {
                $data = [
                    'status' => false,
                    'info' => '会员' . $id . '禁用成功'
                ];
                $redis = Myredis::hget('userLogin', $tel['tel']);
                if ($redis) {
                    $redis['status'] = 0;
                    $res = Myredis::hset('userLogin', $tel['tel'], $redis);
                }
            } else {
                $data = [
                    'status' => false,
                    'info' => '网络错误'
                ];
            }

        }
        return json($data);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {

    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function read($id)
    {
        if (!Request::instance()->isAjax()) {
            return $this->error('小伙子迷了路', url('admin/index/index'));
        }
        $res = Db::table('xo_user')->field('pwd', true)->find($id);
        if ($res) {
            return json($res);
        } else {
            return null;
        }
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $res = Db::table('xo_user')->field('pwd', true)->find($id);
        if ($res) {
            return view('edit', [
                'title' => '编辑用户',
                'res' => $res
            ]);
        } else {
            return $this->error('网络错误.......');
        }

    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request $request
     * @param  int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        if (!Request::instance()->put()) {
            return $this->error('小伙子是不是迷了路');
        }
        $id = Request::instance()->put('id');
        $data = [
            'nickname' => Request::instance()->put('nickname'),
            'sign' => Request::instance()->put('sign'),
            'sex' => Request::instance()->put('sex'),
            'birthday' => Request::instance()->put('birthday'),
            'email' => Request::instance()->put('email'),
            'qq' => Request::instance()->put('qq'),
        ];
        $res = Db::table('xo_user')->where(['id' => $id])->update($data);
        if ($res) {
            return $this->success('编辑成功', url('admin/member/index'));
        } else {
            return $this->error('编辑失败');
        }
    }

    /**
     * 删除指定资源
     * @param  int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        if (!Request::instance()->isDelete()) {
            return $this->error('小伙子,迷了路....', url('admin/index/index'));
        }
        Db::startTrans();
            $res = Db::table('xo_user_friends')->where(['uid'=>$id])->delete();
            $res =Db::table('xo_user_friends')->where(['fid'=>$id])->delete();
            $res =Db::table('xo_user_goods')->where(['uid'=>$id])->delete();
            $res =Db::table('xo_user_down')->where(['uid'=>$id])->delete();
            $res =Db::table('xo_goods_up')->where(['uid'=>$id])->delete();
            $res = Db::table('xo_user')->where(['id' => $id])->delete();
        if ($res){
            $data = [
                'status' => true,
                'info' => '会员' . $id . '已经被删除成功'
            ];
            Db::commit();
            return json($data);
        }else{
            $data = [
                'status' => false,
                'status' => '会员' . $id . '删除失败'
            ];
            Db::rollback();
            return json($data);
        }

    }
}
